package com.easybuy.dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

//import javax.naming.Context;
//import javax.naming.InitialContext;
//import javax.naming.NamingException;
//import javax.sql.DataSource;

/**
 * BaseDao宸ュ叿绫�
 * @author Administrator
 *
 */
public class BaseDao {
	private static String driver;			//鍔犺浇椹卞姩
	private static String url;				//璺緞
	private static String user;				//鐢ㄦ埛鍚�
	private static String password;			//瀵嗙爜
	
	/**
	 * 闈欐�鍒濆鍖�
	 */
	static{
		init();
	}
	
	/**
	 * 鍒濆鍖�
	 */
	public static void init(){
		//鍒涘缓Properties瀵硅薄
		Properties pro = new Properties();
		//鑾峰彇璺緞
		String fileName = "database.properties";
		//鍒涘缓InputStream瀵硅薄鎺ユ敹璺緞
		InputStream is = BaseDao.class.getClassLoader().getResourceAsStream(fileName);
		//寮傚父澶勭悊
		try {
			
			pro.load(is);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//鑾峰彇鏂囦欢鐨勫唴瀹怪�
		driver = pro.getProperty("driver");
		url = pro.getProperty("url");
		user = pro.getProperty("user");
		password = pro.getProperty("password");
	}
	
	/**
	 * 鑾峰彇杩炴帴瀵硅薄
	 * @return
	 */
	public Connection getConnection(){
		//鍒涘缓Connection瀵硅薄
		Connection conn = null;
//		//鍒涘缓Context瀵硅薄
//		Context ctx=null;
//		try {
//			ctx = new InitialContext();
//			//鑾峰彇鏁版嵁璧勬簮
//			DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/news");
//			//杩炴帴鏁版嵁搴�
//			conn=ds.getConnection();
//		} catch (NamingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		//濡傛灉杩炴帴瀵硅薄涓虹┖鏃�
		if(conn == null){
			//寮傚父澶勭悊
			try {
				//鍔犺浇椹卞姩
				Class.forName(driver);
				//鑾峰彇杩炴帴瀵硅薄
				conn = DriverManager.getConnection(url, user, password);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return conn;
	}
	
	/**
	 * 鍏抽棴鏁版嵁搴�
	 * @param conn 杩炴帴瀵硅薄
	 * @param stmt Statement瀵硅薄
	 * @param rs 缁撴灉闆嗗璞�
	 */
	public void closeAll(Connection conn,Statement stmt,ResultSet rs){
		//鍒ゆ柇缁撴灉闆嗘槸鍚︿负绌�
		if(rs != null){
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		//鍒ゆ柇Statement鏄惁涓虹┖
		if(stmt != null){
			try {
				stmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}		
		//鍒ゆ柇杩炴帴瀵硅薄鏄惁涓虹┖
		if(conn != null){
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 澧炪�鍒犮�鏀规搷浣�
	 * @param sql sql璇彞
	 * @param params 鍙傛暟鏁扮粍
	 * @return  鍙楀奖鍝嶈鏁�
	 */
	protected int executeUpdate(String sql,Object[] params){
		//鍒涘缓PreparedStatement瀵硅薄
		PreparedStatement pstmt = null;
		//瀹氫箟涓�釜鍙橀噺
		int num = 0;
		//杩炴帴鏁版嵁搴�
		Connection conn = getConnection();
		//寮傚父澶勭悊
		try {
			pstmt = conn.prepareStatement(sql);
			if(params != null){
				//寰幆
				for(int i = 0; i < params.length; i++){
					//缂栧啓sql璇彞
					pstmt.setObject(i+1, params[i]);
				}
			}
			num = pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeAll(conn,pstmt,null);
		}
		return num;
	}
}
